package com.qf.authcode;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.response.AlipayTradePayResponse;
import com.qf.mch.MchConfig;

/**
 * 支付宝官方文档地址
 * https://opendocs.alipay.com/open/194/106039?pathHash=5b8cf9e6
 */
public class PayTest {

    public static final String orderNum = "202409261010002";
    public static final String auth_code = "288284241191595705"; // 支付宝条码

    public static void main(String[] args) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(MchConfig.serverurl, MchConfig.appid, MchConfig.privatekey, "json", "UTF-8", MchConfig.publickey, "RSA2");
        AlipayTradePayRequest request = new AlipayTradePayRequest();
//异步接收地址，仅支持http/https，公网可访问
        request.setNotifyUrl("");

/******必传参数******/
        JSONObject bizContent = new JSONObject();
//商户订单号，商家自定义，保持唯一性
        bizContent.put("out_trade_no", orderNum);
//支付金额，最小值0.01元
        bizContent.put("total_amount", 0.98);
//订单标题，不可使用特殊符号
        bizContent.put("subject", "测试商品");
//1.条码场景：bar_code；2.刷脸场景：security_code
        bizContent.put("scene", "bar_code");
//根据auth_code_type上传付款码，auth_code_type=bar_code，则填写用户支付宝钱包中的付款码；auth_code_type=security_code，则填写设备刷脸返回的付款码；
        bizContent.put("auth_code", auth_code);

/******可选参数******/
//当面付场景下，当面付快捷版则传 OFFLINE_PAYMENT;其它支付宝当面付产品传 FACE_TO_FACE_PAYMENT；不传则默认使用FACE_TO_FACE_PAYMENT。
//bizContent.put("product_code", "FACE_TO_FACE_PAYMENT");
//// 商品明细信息
//JSONArray goodsDetail = new JSONArray();
//JSONObject goods1 = new JSONObject();
//goods1.put("goods_id", "goodsNo1");
//goods1.put("goods_name", "子商品1");
//goods1.put("quantity", 1);
//goods1.put("price", 0.01);
//goodsDetail.add(goods1);
//bizContent.put("goods_detail", goodsDetail);

//// 扩展信息
//JSONObject extendParams = new JSONObject();
//extendParams.put("sys_service_provider_id", "2088511833207846");
//bizContent.put("extend_params", extendParams);

//// 返回参数选项
//JSONArray queryOptions = new JSONArray();
//queryOptions.add("fund_bill_list");
//queryOptions.add("voucher_detail_list");
//bizContent.put("query_options", queryOptions);

        request.setBizContent(bizContent.toString());
        AlipayTradePayResponse response = alipayClient.execute(request);
        System.out.println(JSON.toJSONString(response));
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }
}
